/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.mdc.hearts.ejb.gameLogic;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import static it.mdc.hearts.ejb.gameLogic.GlobalConstants.*;

/**
 *
 * @author Administrator
 */
@Stateless
@EJB(name="java:global/HeartsSessionBean", beanInterface=HeartsSessionBeanLocal.class)
public class HeartsSessionBean implements HeartsSessionBeanLocal {

    @Override
    public HeartsModel initHeartsModel() {
        HeartsModel model = new HeartsModel();
        return model;
    }
    
    @Override
    public boolean playCard(HeartsModel model, int nPlayerIndex, Card card)
    {
        boolean bRet = false;
        try {
            model.mutex.acquire();
            if(model.nCurrentPlayer == nPlayerIndex)
            {
                ArrayList<Card> playableCards = getPlayableCards(model, nPlayerIndex);
                
                if(playableCards.remove(card) && model.playerDecks[nPlayerIndex].cardsDeck.remove(card))
                {
                    model.playerCardPlayed[nPlayerIndex] = card;
                    
                    if((model.nGameTurnStep == 0))
                    {
                        model.nGameTurnSeed = card.getSeed();
                    }
                    
                    if(card.getSeed() == HEARTS)
                    {
                        model.bHeartBreak = true;
                    }
                    
                    bRet = true;
                }
            }
            model.mutex.release();
        } catch (InterruptedException ex) 
        {
             return bRet;
        }
        return bRet;
    }
    
    @Override
    public ArrayList<Card> getPlayableCards(HeartsModel model, int playerIndex) {
         
         boolean bOnlyHearts = true;
         ArrayList<Card> playableCards = new ArrayList<Card>();
         
         for(int i = 0; i < model.playerDecks[playerIndex].cardsDeck.size(); i++)
         {
             Card card = model.playerDecks[playerIndex].cardsDeck.get(i);
             
             if(card.getSeed() != HEARTS)
             {
                 bOnlyHearts = false;
             }
             
             if((model.nGameTurnStep == 0) && (model.nGameTurn == 0))
             {
                 if((card.getValue() == 0) && (card.getSeed() == CLUBS))
                 {
                     playableCards.add(card);
                     break;
                 }
             }
             else
             {
                if(card.getSeed() == model.nGameTurnSeed)
                {
                    playableCards.add(card);
                }  
             }
         }
         
         if(playableCards.isEmpty())
         {
             for(int i = 0; i < model.playerDecks[playerIndex].cardsDeck.size(); i++)
             {
                 Card card = model.playerDecks[playerIndex].cardsDeck.get(i);
                 
                 if(card.getSeed() == HEARTS)
                 {
                     if((model.bHeartBreak) || (bOnlyHearts) || ((model.nGameTurnStep != 0) && (model.nGameTurn != 0)))
                     {
                         playableCards.add(card);
                     }
                 }
                 else if((card.getSeed() == SPADES) && (card.getValue() == 10))
                 {
                     if(model.nGameTurn != 0)
                     {
                        playableCards.add(card);
                     }
                 }
                 else
                 {
                     playableCards.add(card);
                 }
             }
         }
         
         if(playableCards.isEmpty())
         {
             System.out.println("cazzo niente carte");
         }
         
         return  playableCards;
    }
     
    private int getTurnStatus(HeartsModel model) 
    {
        int nStatus = NEXT_STEP;
        
        if(model.nGameTurn == 13)
        {
            nStatus = END_GAME;
        }
        else if(model.nGameTurnStep == 3)
        {
            nStatus = END_TURN;
        }
        
        return nStatus;
    }

    @Override
    public int evaluateStep(HeartsModel model) 
    {
        int nRet = 0;
        try {
           model.mutex.acquire();
           if(model.nGameTurnStep == 3)
           {
               //Valutare chi ha vinto
               int nPlayerIndex = 0;
               int nMaxCardValue = -1;
               int nHandPoint = 0;
               ArrayList<Card> handCard = new ArrayList<Card>();
               
               for(int i = 0; i < PLAYER_NUMBER; i++)
               {    
                   Card card = model.playerCardPlayed[i];
                   if(card.getPoint() > 0)
                   {
                        handCard.add(card);
                        nHandPoint += card.getPoint();
                   }
                   if((model.nGameTurnSeed == card.getSeed()) && (nMaxCardValue < card.getValue()))
                   {
                       nPlayerIndex = i;
                       nMaxCardValue = card.getValue();
                   }
                   model.playerCardPlayed[i] = null;
               }
               model.nCurrentPlayer = nPlayerIndex;
               model.playerCardsTaken[nPlayerIndex].cardsDeck.addAll(handCard);
               
               model.playerPoint[nPlayerIndex] += nHandPoint;
               if(model.playerPoint[nPlayerIndex] >= 26)
               {
                   for(int i = 0; i < 4; i++)
                   {
                       model.playerPoint[i] = 26;
                   }
                   model.playerPoint[nPlayerIndex] = 0;
               }
               
               model.nGameTurn++;
               
               nRet = getTurnStatus(model);
               
               model.nGameTurnStep = 0;
               model.nGameTurnSeed = -1;
           }
           else
           {
               model.nCurrentPlayer = (model.nCurrentPlayer + 1) % 4;
               
               nRet = getTurnStatus(model);
               
               model.nGameTurnStep++;
           }
           model.mutex.release();
        } 
        catch (InterruptedException ex) {
            Logger.getLogger(HeartsSessionBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nRet;
    } 
}
